package com.english.numbers;

import com.english.numbers.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Start extends Activity {
    /** Called when the activity is first created. */
    AlertDialog.Builder builder;
    boolean demo = false; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Eula.show(this);
     	setContentView(R.layout.start);
     	if(this.demo==true){
     		TextView name = (TextView) findViewById(R.id.start_appname);
     		name.setText(name.getText()+" "+getResources().getString(R.string.demo));
     	}
    	Button startLevel0 = (Button) findViewById(R.id.start_startLearn0);
    	Button startLevel1 = (Button) findViewById(R.id.start_startLearn1);
    	Button startLevel2 = (Button) findViewById(R.id.start_startLearn2);
    	Button startLevel3 = (Button) findViewById(R.id.start_startLearn3);
    	Button howto = (Button) findViewById(R.id.start_howToPlay);
    	Button results = (Button) findViewById(R.id.start_records);
    	Button about = (Button) findViewById(R.id.start_about);
    	Button exit = (Button) findViewById(R.id.start_exit);
    	
    	startLevel0.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        	setPreferencesForTraining(0);
        	Intent myIntent = new Intent(Start.this, Training.class);
        	Start.this.startActivity(myIntent);
        	Start.this.finish();
             }
         });
    	startLevel1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(demo == true ){
            		showDemoDialog();
            	}else{
	            	setPreferencesForTraining(1);
	            	Intent myIntent = new Intent(Start.this, Training.class);
	            	Start.this.startActivity(myIntent);
	            	Start.this.finish();
            	}
            }
        });
    	startLevel2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(demo == true ){
            		showDemoDialog();
            	}else{
	            	setPreferencesForTraining(2);
	            	Intent myIntent = new Intent(Start.this, Training.class);
	            	Start.this.startActivity(myIntent);
	            	Start.this.finish();
            	}
	            }
        });
    	startLevel3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	if(demo == true ){
            		showDemoDialog();
            	}else{
	            	setPreferencesForTraining(3);
	            	Intent myIntent = new Intent(Start.this, Training.class);
	            	Start.this.startActivity(myIntent);
	            	Start.this.finish();
            	}
            }
        });
    	builder = new AlertDialog.Builder(this);
    	exit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
	            	builder.setMessage(getResources().getString(R.string.exitMessage))
	                .setCancelable(false)
	                .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int id) {
	                         Start.this.finish();
	                    }
	                })
	                .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int id) {
	                         dialog.cancel();
	                    }
	                });
            		AlertDialog alert = builder.create();
            		alert.show();
            }
        });
       	about.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent myIntent = new Intent(Start.this, About.class);
            	Start.this.startActivity(myIntent);
            	Start.this.finish();
            }
        });
      	howto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent myIntent = new Intent(Start.this, HowTo.class);
            	Start.this.startActivity(myIntent);
            	Start.this.finish();
            }
        });
     	results.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent myIntent = new Intent(Start.this, Results.class);
            	Start.this.startActivity(myIntent);
            	Start.this.finish();
            }
        });
    
    }
    
    private void showDemoDialog(){
    	builder.setMessage(getResources().getString(R.string.demoText))
        .setCancelable(false)
        .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
		AlertDialog alert = builder.create();
		alert.show();
    }
    private void setPreferencesForTraining(int level){
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
       	Integer wordCount = 0;
        Integer wrongCount = 0;
        Integer points = 0;
        SharedPreferences settings = getSharedPreferences("Com.numbers.learn", 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("level", level);
        editor.putInt("wordCount", wordCount);
        editor.putInt("wrongCount", wrongCount);
        editor.putInt("points", points);
        editor.putBoolean("gameFinished", false);
        editor.putBoolean("demo", this.demo);

        // Commit the edits!
        editor.commit();
    }
    }